<?php
/**
 * Clavid OAUTH SuisseID example.
 * 
 * @version $Id: clavid_suisseid_request.php 6 2010-05-17 09:19:49Z telbie $
 */

/* Start session and load library. */
session_start();
require_once('clavid_suisseid_config.php');
require_once('clavidoauth/ClavidOAuthSuisseID.php');

/* Build ClavidOAuthSuisseID object with client credentials. */
$connection = new ClavidOAuthSuisseID(CONSUMER_KEY, CONSUMER_SECRET);

/* Set the user language */
$connection->setUserLanguage('de');

/* Set the required attributes */
$connection->setRequiredAttribute(ClavidOAuthSuisseID::SuisseID_Number);
$connection->setOptionalAttribute(ClavidOAuthSuisseID::SuisseID_Name);
$connection->setOptionalAttribute(ClavidOAuthSuisseID::SuisseID_EMail);
 
/* Get temporary credentials. */
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);

/* Save temporary credentials to session. */
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
 
/* If last connection failed don't display authorization link. */
switch ($connection->http_code) {
  case 200:
    /* Build authorize URL and redirect user to Clavid. */
    $url = $connection->getAuthorizeURL($token);
    header('Cache-Control: no-cache');
    header('Pragma: no-cache');
    header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
    header('Location: '.$url);
    break;
  default:
    /* Show notification if something went wrong. */
    echo 'Could not connect to Clavid. Refresh the page or try again later.';
}

?>
